package com.im.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wangxh
 * @ClassName: StringUtil
 * @Description: 字符串工具类
 * @date 2017/5/15 14:47
 */
public class StringUtil {
	/**
	 * 判断字符串是否为null或者空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return str == null || "".equals(str);
	}

	/**
	 * 判断对象是否为null或者空
	 * 
	 * @param object
	 * @return
	 */
	public static boolean isObjectEmpty(Object object) {
		String str ="";
		if(object!=null){
			str =  object.toString();
		}
		return isEmpty(str);
	}

	/**
	 * 字符串替换

	 * 
	 * @param text
	 *            原始字符串

	 * @param repl
	 *            想被替换的内容

	 * @param with
	 *            替换后的内容
	 * @return
	 */
	public static String replace(String text, String repl, String with) {
		if (text == null || repl == null || with == null || repl.length() == 0) {
			return text;
		}

		StringBuffer buf = new StringBuffer(text.length());
		int searchFrom = 0;
		while (true) {
			int foundAt = text.indexOf(repl, searchFrom);
			if (foundAt == -1) {
				break;
			}

			buf.append(text.substring(searchFrom, foundAt)).append(with);
			searchFrom = foundAt + repl.length();
		}
		buf.append(text.substring(searchFrom));

		return buf.toString();
	}
	
	/**
	 * 将为null的对象转换为空字符串
	 * */
	public static String getNoNullStr(Object str){
		return str==null?"":str.toString();
	}
	
	/**
	 * 将为null的对象转换为空字符串
	 * */
	public static Long getNoNullLong(Object str){
		return str == null?null:Long.parseLong(str.toString());
	}

	public static Double getNoNullDouble(Object str) {
		return str == null ? 0D : Double.parseDouble(str.toString());
	}
	
	/**
	 * 获取指定长度(按字节长度获取)的字符串，中文算2个字节长度，兼容oracle的
	 * varchar2长度计算方式
	 * @param src 源字符串
	 * @param length 长度
	 * @return
	 */
	public static String getSubStr(String src, int length) {
		if (StringUtil.isEmpty(src)) {
			return null;
		}
		byte[] b = src.getBytes();
		if (b.length > length) {
			byte[] s = new byte[length];
			for (int i = 0; i < length; i++) {
				s[i] = b[i];
			}
			return new String(s);
		} else {
			return src;
		}
	}
	
	  public static boolean isBlank(CharSequence cs)
	  {
	    int strLen;
	    if ((cs == null) || ((strLen = cs.length()) == 0))
	      return true;
	    for (int i = 0; i < strLen; i++) {
	      if (!Character.isWhitespace(cs.charAt(i))) {
	        return false;
	      }
	    }
	    return true;
	  }

	  public static boolean isNotBlank(CharSequence cs)
	  {
	    return !isBlank(cs);
	  }
/**
 * 校验是否为手机号码
 * @param mobiles
 * @return
 */
	public static boolean isMobileNO(String mobiles){  
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,3-9]))\\d{8}$");  
		Matcher m = p.matcher(mobiles);  
		return m.matches();  

	}
	
	/**
	 * 左边补位
	 * @param length
	 * @param val
	 * @param left
	 * @return
	 */
	public static String leftFillStr(int length, long val, String left){
    	StringBuffer str = new StringBuffer();
        int len = String.valueOf(val).length();
        if(len<length){
            for(int i =0;i<length - len; i++){
                str.append(left);
            }
            str.append(val);
        } else {
            str.append(val);
        }
        return str.toString();
    }
}
